package com.guashu.jaywayjsonpath.renhang;

import com.alibaba.fastjson.JSON;
import com.guashu.jaywayjsonpath.util.DealMessageUtil;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import net.minidev.json.JSONArray;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InsertData_pboc_acc_repay_60_monthly {
    public static void main(String[] args) {
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/renhang?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC";
        String jsonFilePath = "E:\\国银租赁\\需求\\20240201-人行指标\\人行报文.json"; // JSON文件路径
        PreparedStatement pstmt = null;
        Connection conn = null;
        try {
            System.out.println("开始解析人行报文...");
            String json = new String(Files.readAllBytes(Paths.get(jsonFilePath)), StandardCharsets.UTF_8);
            conn = DriverManager.getConnection(url, user, password);
            conn.setAutoCommit(false);

            String sql = "INSERT INTO pboc_acc_repay_60_monthly (pd01ad01, pd01bd01, pd01ai01, pd01er03, pd01ed01, pd01ej01, pa01bi01, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, now(), now())";
            pstmt = conn.prepareStatement(sql);

            // 解析规则封装到Map中
            Map<String, String> fieldJsonPaths = new HashMap<>();
            fieldJsonPaths.put("pa01bi01", "$.PRH.PA01.PA01B.PA01BI01");
            fieldJsonPaths.put("pd01ad01", "$.PDA.PD01[*].PD01A.PD01AD01");
            fieldJsonPaths.put("pd01bd01", "$.PDA.PD01[*].PD01B.PD01BD01");
            fieldJsonPaths.put("pd01ai01", "$.PDA.PD01[*].PD01A.PD01AI01");
            fieldJsonPaths.put("pd01er03", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ER03");
            fieldJsonPaths.put("pd01ed01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01ED01");
            fieldJsonPaths.put("pd01ej01", "$.PDA.PD01[*].PD01E.PD01EH[*].PD01EJ01");
            String pa01bi01 = DealMessageUtil.safelyReadJsonPath(json, fieldJsonPaths.get("pa01bi01"));
            JSONArray pd01Records = JsonPath.read(json, "$.PDA.PD01[*]");
            for (Object pd01Record : pd01Records) {
                String pd01RecordJson = JSON.toJSONString(pd01Record); // 将单条记录转换为JSON字符串
                String pd01ad01 = DealMessageUtil.safelyReadJsonPath(pd01RecordJson, "$.PD01A.PD01AD01");
                String pd01bd01 = DealMessageUtil.safelyReadJsonPath(pd01RecordJson, "$.PD01B.PD01BD01");
                String pd01ai01 = DealMessageUtil.safelyReadJsonPath(pd01RecordJson, "$.PD01A.PD01AI01");
                List<Object> pd01EHRecords = null;
                try {
                    // 尝试读取PD01EH数组
                    pd01EHRecords = JsonPath.read(pd01RecordJson, "$.PD01E.PD01EH[*]");
                } catch (PathNotFoundException e) {
                    pstmt.setString(1, pd01ad01);
                    pstmt.setString(2, pd01bd01);
                    pstmt.setString(3, pd01ai01);
                    pstmt.setString(4, null);
                    pstmt.setString(5, null);
                    pstmt.setString(6, null);
                    pstmt.setString(7, pa01bi01);
                    pstmt.addBatch();
                    continue;
                }

                for (Object pd01EHRecord : pd01EHRecords) {
                    String pd01EHRecordJson = JSON.toJSONString(pd01EHRecord);
                    String pd01er03 = DealMessageUtil.safelyReadJsonPath(pd01EHRecordJson, "$.PD01ER03");
                    String pd01ed01 = DealMessageUtil.safelyReadJsonPath(pd01EHRecordJson, "$.PD01ED01");
                    String pd01ej01 = DealMessageUtil.safelyReadJsonPath(pd01EHRecordJson, "$.PD01EJ01");
                    pstmt.setString(1, pd01ad01);
                    pstmt.setString(2, pd01bd01);
                    pstmt.setString(3, pd01ai01);
                    pstmt.setString(4, pd01er03);
                    pstmt.setString(5, pd01ed01);
                    pstmt.setString(6, pd01ej01);
                    pstmt.setString(7, pa01bi01);
                    // 执行SQL语句
                    pstmt.addBatch();
                }
            }
            // Execute batch
            pstmt.executeBatch();
            conn.commit();

            // Close resources
            pstmt.close();
            conn.close();
            System.out.println("解析人行报文入库完成!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                pstmt.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
